﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace BOSS.Web.Extensions
{
    /// <summary>
    /// HtmlHelper 的导航扩展
    /// </summary>
    public static class NavigationExtensions
    {
        /// <summary>
        /// 生成导航面板
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="dataSource"></param>
        /// <returns></returns>
        public static string NavigatePanels(this HtmlHelper helper, SiteMapDataSource dataSource)
        {
            SiteMapProvider provider = dataSource.Provider;
            StringBuilder builder = new StringBuilder();

            builder.Append("[\n");

            if (provider.RootNode != null)
            {
                int panelCount = 0;
                foreach (SiteMapNode node in provider.RootNode.ChildNodes)
                {
                    foreach (string role in node.Roles)
                    {
                        if ((role == "*") || Roles.IsUserInRole(role))
                        {
                            AppendPanel(builder, node);
                            builder.Append(",\n");
                            panelCount++;
                            break;
                        }
                    }
                }
                if (panelCount != 0)
                {
                    builder.Length -= 2;
                }
                else
                {
                    builder.Append("{title:'请重新登录',lines:false,rootVisible:true,root:{text:'请重新登录', href:'Account/Login', leaf:true}}");
                }
            }
            else
            {
                builder.Append("{title:'登录系统',lines:false,rootVisible:true,root:{text:'登录系统', href:'Account/Login', leaf:true}}");
            }

            builder.Append("]");

            return builder.ToString();
        }

        /// <summary>
        /// 添加一个面板
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="node"></param>
        private static void AppendPanel(StringBuilder builder, SiteMapNode node)
        {
            builder.AppendFormat("{{title:'{0}',", node.Title);
            if (!string.IsNullOrEmpty(node.Description))
            {
                builder.AppendFormat("tabTip:'{0}',", node.Description);
            }
            // builder.Append("lines:false,rootVisible:true,root:");
            builder.Append("lines:false,rootVisible:false,root:");
            AppendTreeNode(builder, node);
            builder.Append("}");
        }

        /// <summary>
        /// 添加一个树节点
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="node"></param>
        private static void AppendTreeNode(StringBuilder builder, SiteMapNode node)
        {
            if (string.IsNullOrEmpty(node.Description))
            {
                builder.AppendFormat("{{text:'{0}',", node.Title);
            }
            else
            {
                builder.AppendFormat("{{text:'{0}',qtip:'{1}',", node.Title, node.Description);
            }

            bool hasChildNodes = false;
            foreach (SiteMapNode child in node.ChildNodes)
            {
                // 子节点未指定权限，则直接继承父节点的权限
                if (child.Roles.Count == 0)
                {
                    if (hasChildNodes == false)
                    {
                        builder.Append("leaf:false,singleClickExpand:true,expanded:true,children:[");
                        hasChildNodes = true;
                    }
                    AppendTreeNode(builder, child);
                    builder.Append(",");
                }
                else
                {
                    foreach (string role in child.Roles)
                    {
                        if ((role == "*") || Roles.IsUserInRole(role))
                        {
                            if (hasChildNodes == false)
                            {
                                builder.Append("leaf:false,singleClickExpand:true,expanded:true,children:[");
                                hasChildNodes = true;
                            }
                            AppendTreeNode(builder, child);
                            builder.Append(",");
                            break;
                        }
                    }
                }
            }
            if (hasChildNodes)
            {
                builder.Length--;
                builder.Append("]}");
            }
            else
            {
                builder.AppendFormat("href:'{0}',leaf:true}}", node.Url);
            }
        }
    }
}
